
package results;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * FOR TEST PURPOSES ONLY.
 *
 * @author gael, guillaume
 * @created May 15, 2004
 */

public class ExerciseSerializable {

 public static void main(String[] aArguments) {
	//create a Serializable List
	List quarks = new ArrayList();
	quarks.add("up");
	quarks.add("down");
	quarks.add("strange");
	quarks.add("charm");
	quarks.add("top");
	quarks.add("bottom");

	//serialize the List
	//note the use of generic interface references

	//declared here only to ensure visibilty in finally clause
	ObjectOutput output = null;
	try{
	  //use buffering
	  OutputStream file = new FileOutputStream( "quarks.ser" );
	  OutputStream buffer = new BufferedOutputStream( file );
	  output = new ObjectOutputStream( buffer );
	  output.writeObject(quarks);
	}
	catch(IOException ex){
	  fLogger.log(Level.SEVERE, "Cannot perform output.", ex);
	}
	finally{
	  try {
		if (output != null) {
		  //flush and close "output" and its underlying streams
		  output.close();
		}
	  }
	  catch (IOException ex ){
		fLogger.log(Level.SEVERE, "Cannot close output stream.", ex);
	  }
	}

	//deserialize the quarks.ser file
	//note the use of generic interface references

	//declared here only to ensure visibilty in finally clause
	ObjectInput input = null;
	try{
	  //use buffering
	  InputStream file = new FileInputStream( "quarks.ser" );
	  InputStream buffer = new BufferedInputStream( file );
	  input = new ObjectInputStream ( buffer );
	  //deserialize the List
	  ArrayList recoveredQuarks = (ArrayList)input.readObject();
	  //display its data
	  Iterator quarksItr = recoveredQuarks.iterator();
	  while ( quarksItr.hasNext() ) {
		System.out.println( (String)quarksItr.next() );
	  }
	}
	catch(IOException ex){
	  fLogger.log(Level.SEVERE, "Cannot perform input.", ex);
	}
	catch (ClassNotFoundException ex){
	  fLogger.log(Level.SEVERE, "Unexpected class found upon input.", ex);
	}
	finally{
	  try {
		if ( input != null ) {
		  //close "input" and its underlying streams
		  input.close();
		}
	  }
	  catch (IOException ex){
		fLogger.log(Level.SEVERE, "Cannot close input stream.", ex);
	  }
	}
  }

  // PRIVATE //
	
  //Use Java's logging facilities to record exceptions.
  //The behavior of the logger can be configured through a
  //text file, or programmatically through the logging API.
  private static final Logger fLogger =
				Logger.getLogger(ExerciseSerializable.class.getPackage().getName());

} 
